﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace DotNetStar.TheKing.Web.Pages.Content
{
    using Services;
    using Infrastructure;
    using DotNetStar.TheKing.Models;
    using DotNetStar.TheKing.Domain;

    public class SearchModel : PageModel
    {
        private readonly ContentService _contentService;
        private readonly TagService _tagService;
        public SearchModel(ContentService contentService,TagService tagService)
        {
            _contentService = contentService;
            _tagService = tagService;
        }

        /// <summary>
        /// 搜索结果
        /// </summary>
        public PagedCollection<Domain.Content> SearchResult { get; set; }

        public IEnumerable<Tag> Tags { get; set; }

        public string SearchedKeyword { get; private set; }

        public async Task<IActionResult> OnGetAsync(string keyword)
        {
            var page = Request.Query["page"].ToString().To(1);

            this.SearchResult = await _contentService.GetContentsAsync(new ContentPageQuery(page)
            {
                Keyword = keyword
            });
            this.SearchedKeyword = keyword;

            this.Tags = await _tagService.GetHostTagsAsync();

            return Page();
        }
    }
}